﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class DzialController : Controller
    {
        //
        // GET: /Dzial/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {
                return View(context.Dzialy.Select(s => new DzialViewModel
                {
                    ID = s.ID_Dzial,
                    Nazwa = s.Nazwa,  
                    DataM = s.DataM,
                    DataU = s.DataU
                
                }).ToArray()
                );
            }
        }

        public ActionResult Create()
        {
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(DzialViewModel d)
        {
            using (var context = new HREntities())
            {
                context.Dzialy.AddObject(d.GetModel(new Dzial()));
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                var d = context.Dzialy.SingleOrDefault(s => s.ID_Dzial == id);
                if (d == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new DzialViewModel(d));
            }
        }

        [HttpPost]
        public ActionResult Edit(DzialViewModel d)
        {
            using (var context = new HREntities())
            {
                var dzialDB = context.Dzialy.SingleOrDefault(s => s.ID_Dzial == d.ID);
                context.ApplyCurrentValues<Dzial>("Dzialy", d.GetModel(dzialDB));
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                var dzial = context.Dzialy.SingleOrDefault(s => s.ID_Dzial == id);
                if (dzial != null)
                {
                    context.Dzialy.DeleteObject(dzial);
                    context.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Delete(DzialViewModel[] dzialy)
        {
            using (var context = new HREntities())
            {
                foreach (var item in dzialy)
                {
                    if (item.CzyZaznaczone)
                    {
                        var dzial = context.Dzialy.SingleOrDefault(s => s.ID_Dzial == item.ID);
                        if (dzial != null)
                        {
                            context.Dzialy.DeleteObject(dzial);                           
                        }
                    }
                }
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                DzialViewModel dzial = new DzialViewModel(context.Dzialy.SingleOrDefault(s => s.ID_Dzial == id));
                if (dzial == null)
                {
                    return RedirectToAction("Error");
                }
                return View(dzial);
            }
        }

    }
}
